#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;

int getDays(const vector<int>& weights, int capacity) {
    int ans = 1;
    int c = capacity;
    for(int w : weights){
        if(c >= w){
            c -= w;
        }else{
            ans++;
            c = capacity - w;
        }
    }
    return ans;
}

int shipWithinDays(vector<int>& weights, int days) {
    int l = *max_element(weights.begin(), weights.end());
    int r = accumulate(weights.begin(), weights.end(), 0);
    int mid;
    while (l <= r) {
        mid = l + (r - l) / 2;
        if (getDays(weights, mid) <= days) {
            r = mid - 1;
        } else {
            l = mid + 1;
        }
    }
    return l;
}